package 堆栈与队列;

import javax.lang.model.element.VariableElement;
import java.util.Arrays;

public class 最小栈 {

    private int[] data;
    private int[] minData;
    private int dataKey;
    private int minDataKey;

    /**
     * initialize your data structure here.
     */
    public 最小栈() {
        data = new int[10];
        minData = new int[10];
        dataKey = -1;
        minDataKey = -1;
    }

    public void push(int x) {
        dataKey++;
        if (dataKey >= data.length) {
            data = resize(data);
        }
        data[dataKey] = x;

        if (minDataKey < 0 || x <= minData[minDataKey]) {
            minDataKey++;
            if (minDataKey >= minData.length) {
                minData = resize(minData);
            }
            minData[minDataKey] = x;
        }
    }

    public void pop() {
        if (data[dataKey] <= minData[minDataKey]) {
            minDataKey--;
        }
        dataKey--;

    }

    public int top() {
        return data[dataKey];
    }

    public int getMin() {
        return minData[minDataKey];
    }

    public int[] resize(int[] nums) {
        return Arrays.copyOf(nums, nums.length * 2);
    }
}
